<?php
namespace app\index\controller;
use app\index\Model\Danci as DanciModel;
use app\index\Model\LearnUser;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        //return '学单词';
        if(request()->method()=="GET" && input('get.word')){
            $keyword=input('get.word');
            $model=new DanciModel();
            $result=$model->where('words','like',$keyword.'%')->select();
            $this->assign('result',$result);
        }else{
            $this->assign('result',NULL);
        }
        return $this->fetch();
    }
    public function learn(){
        //引入单词模型
        $model= new DanciModel();
        //$result=DanciModel::get(1);
        //随机出现100个单词
        for ($i=1;$i<100;$i++) {
            $tiid=mt_rand(1, 4000);
            $result[$i]=$model->where(array('id'=>$tiid))->find();
        }
        $this->assign('result',$result);
        return $this->fetch();
    }
    //获取所有单词
    public function all(){
        //引入单词模型
        $knowcount=db('learn_user')->where('userid',1)->count();
        //$this->assign('know',$knowcount);
        $model= new DanciModel();
        //$result=DanciModel::get(1);
        //随机出现100个单词
        $result=$model->paginate(50);
        $this->assign('allword',round($knowcount/$result->total()*100,2));
        $this->assign('result',$result);
        return $this->fetch();
    }
    //标记知道的单词
    public function knowword(){
        $danciid=input('get.danciid');
        $map['userid']=1;
        $map['danciid']=$danciid;
        if(db('learn_user')->where($map)->find()){
            return "请勿重新添加单词.";
        }
        $model=new LearnUser();
        $model->userid=1;//暂时不考虑用户,默认1
        $model->danciid=$danciid;
        $model->know1=1;//默认会就是1
        if($model->save()){
            $res="已添加到认识的词库.";
        }else{
            $res="保存失败";
        }
        return $res;
    }
}
